package tech.eyao.hs60x.user.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import io.jsonwebtoken.lang.Collections;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import tech.eyao.hs60x.dto.BaseResult;
import tech.eyao.hs60x.enums.ExceptionCodeEnum;
import tech.eyao.hs60x.model.domain.CallRecordsDO;
import tech.eyao.hs60x.service.CallRecordService;

/**
 * 类CallRecordController.java的实现描述：TODO 类实现描述
 * 
 * @author fengjianchao 2018年1月23日 下午9:54:46
 */
@Api("PUSH通话记录")
@RestController
@RequestMapping("/call")
public class CallRecordController {
    private static final Logger logger = LoggerFactory.getLogger(CallRecordController.class);
    @Autowired
    private CallRecordService   callRecordService;

    @RequestMapping(value = "/getCallRecordLogList.do", method = RequestMethod.POST)
    @ApiOperation(value = "获取通话记录", notes = "")
    public @ResponseBody Object getCallRecordLogList(@ApiParam(value = "绑定设备号IMEI") String watchId,
                                                     @ApiParam(value = "来源(非必填)") String source) {
        logger.info(">>getCallRecordLogList start watchId:{},source:{}", watchId, source);
        if (StringUtils.isBlank(watchId)) {
            return BaseResult.isFailure(ExceptionCodeEnum.ERROR_PARAMERROR.getCode(),
                    ExceptionCodeEnum.ERROR_PARAMERROR.getMessage());
        }
        try {
            CallRecordsDO callRecordsDO = new CallRecordsDO();
            callRecordsDO.setWatchId(Long.parseLong(watchId));
            callRecordsDO.setSource(source);
            List<CallRecordsDO> callRecordsDOList = callRecordService.queryCallLogListByConndition(callRecordsDO);
            if (!Collections.isEmpty(callRecordsDOList)) {
                return BaseResult.isSuccess(callRecordsDOList);
            }

        } catch (Exception e) {
            logger.error("getCallRecordLogList error", e.getMessage());
        }
        return BaseResult.isDeFaultNoData();
    }

    /**
     * @param deviceId
     * @param from
     * @param phone
     * @return
     */
    @RequestMapping(value = "/addCallLog.do", method = RequestMethod.POST)
    @ApiOperation(value = "添加通话记录", notes = "")
    public @ResponseBody BaseResult pushCallLog(@ApiParam(value = "终端设备号IMEI") String watchId,
                                                        @ApiParam(value = "呼入关系") String relation,
                                                        @ApiParam(value = "来源(1融云 2移动基站)") String source,
                                                        @ApiParam(value = "电话号码") String phone) {
        try {
            if (StringUtils.isBlank(watchId) || StringUtils.isBlank(phone)) {
                return BaseResult.isFailure(ExceptionCodeEnum.ERROR_PARAMERROR.getCode(),
                        ExceptionCodeEnum.ERROR_PARAMERROR.getMessage());
            }
            CallRecordsDO callRecordsDO = new CallRecordsDO();
            callRecordsDO.setWatchId(Long.parseLong(watchId));
            callRecordsDO.setSource(source);
            callRecordsDO.setRelation(relation);
            callRecordsDO.setPhone(phone);
            return callRecordService.insertCallLog(callRecordsDO);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return BaseResult.isDeFaultNoData();
    }
}
